<?php
namespace app\api\logic;
use think\Db;
use think\Exception;
use app\common\model\{User,UserLevelPay,RechargeOrder,AccountLog,OrderLog,Order};
use app\common\model\ShopConfig;
use app\common\logic\AccountLogLogic;
use app\common\logic\OrderLogLogic;
use app\common\logic\IntegralLogic;
use app\common\logic\DistributionOrderGoodsLogic;

class PayLogic
{
    //购买会员卡回调
    public static function UserLevel_pay($order, $extra = [])
    {
        $time = time();
        $user_num=$order['user_num'];
        //增加会员消费累计额度
        $user = User::get($order['user_id']);
        $time=time();
        $vip_time=time();
        $order_pay_data['type'] =1;
        if($user->level==$order['user_vip_id']){
            if($user->vip_time>=$time){
                $vip_time=$user->vip_time;
            }
            $order_pay_data['type'] =2;
        }
        if($user_num>0){
            $user->vip_time =$vip_time+$user_num*60*60*24;
        }
        $user->total_order_amount = ['inc', $order['order_amount']];
        $user->level =$order['user_vip_id'];

        if (isset($extra['pay_way']) && $extra['pay_way']==3) {
            $user->user_money = ['dec', $order['order_amount']];
            $source_type = AccountLog::balance_vip_order;
            AccountLogLogic::AccountRecord($user['id'],$order['order_amount'],2,$source_type,'购买会员扣减余额',$order['order_id'],$order['order_sn']);
        }

        $user->save();
        //订单更新状态
        $order_pay_data['order_status'] =20;
        $order_pay_data['pay_time'] =$time;
        if (isset($extra['transaction_id'])) {
            $order_pay_data['transaction_id'] = $extra['transaction_id'];
        }
        UserLevelPay::where('id', $order['order_id'])->update($order_pay_data);
    }


    //充值回调
    public static function Recharge_pay($order, $extra = [])
    {
        $time = time();
        //订单更新状态
        $order_pay_data['pay_status'] =1;
        $order_pay_data['pay_time'] =$time;
        if (isset($extra['transaction_id'])) {
            $order_pay_data['transaction_id'] = $extra['transaction_id'];
        }
        RechargeOrder::where('id', $order['order_id'])->update($order_pay_data);

        $user = User::get($order['user_id']);
        $time=time();
        // if (isset($extra['pay_way']) && $extra['pay_way']==2) {
        //     $user->earnings = ['dec', $order['order_amount']];
        //     $source_type = AccountLog::recharge_reduce_earnings;
        //     AccountLogLogic::AccountRecord($user['id'],$order['order_amount'],2,$source_type,'充值余额减少佣金');
        // }
        $total_money = $order['order_amount'];
        $user->user_money = ['inc', $total_money];
        $user->user_integral = ['inc',$order['give_money']];
        $user->total_recharge_amount = ['inc',$total_money];
        $source_type = AccountLog::recharge_money;
        AccountLogLogic::AccountRecord($user['id'],$order['order_amount'],1,$source_type,'用户充值余额',$order['order_id'],$order['order_sn']);

        $source_type1 = AccountLog::recharge_give_integral;
        AccountLogLogic::AccountRecord($user['id'],$order['give_money'],1,$source_type1,'充值赠送积分',$order['order_id'],$order['order_sn']);
        $user->save();
    }

    //商品订单回调
    public static function Order_pay($order, $extra = [])
    {
        //增加会员消费累计额度
        $user = User::get($order['user_id']);
        $user->total_order_amount = ['inc', $order['order_amount']];
        if (isset($extra['pay_way']) && $extra['pay_way']==3) {
            $user->user_money = ['dec', $order['order_amount']];
            $source_type = AccountLog::balance_pay_order;
            AccountLogLogic::AccountRecord($user['id'],$order['order_amount'],2,$source_type,'购买商品扣减余额',$order['order_id'],$order['order_sn']);
        }
        $user->save();

        $order_data['pay_status'] =1;
        $order_data['pay_time'] =time();
        $order_data['order_status'] =1;
        if (isset($extra['transaction_id'])) {
            $order_data['transaction_id'] = $extra['transaction_id'];
        } 
        Order::where('id', $order['order_id'])->update($order_data);

         //订单日志
        OrderLogLogic::record(
            OrderLog::TYPE_USER,
            OrderLog::USER_PAID_ORDER,
            $order['order_id'],
            $user['id'],
            OrderLog::USER_PAID_ORDER
        );

        // 生成分销订单
        DistributionOrderGoodsLogic::add($order['order_id'],1);

        //下单赠送积分
        IntegralLogic::rewardIntegralByGoods($user['id'], $order['order_id']);

    }


    //课程订单回调
    public static function CurriculumOrder_pay($order, $extra = [])
    {
        //增加会员消费累计额度
        $user = User::get($order['user_id']);
        $user->total_order_amount = ['inc', $order['order_amount']];
        if (isset($extra['pay_way']) && $extra['pay_way']==3) {
            $user->user_money = ['dec', $order['order_amount']];
            $source_type = AccountLog::balance_curriculum_order;
            AccountLogLogic::AccountRecord($user['id'],$order['order_amount'],2,$source_type,'购买课程扣减余额');
        }
        $user->save();

        $order_data['pay_status'] =1;
        $order_data['pay_time'] =time();
        $order_data['order_status'] =1;
        if (isset($extra['transaction_id'])) {
            $order_data['transaction_id'] = $extra['transaction_id'];
        } 
        CurriculumOrder::where('id', $order['order_id'])->update($order_data);

        // 生成分销订单
        DistributionOrderGoodsLogic::add($order['order_id'],2);

        //下单赠送积分
        IntegralLogic::rewardIntegralByCurriculum($user['id'], $order['order_id']);

    }
    
}